//
//  GuideViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/7.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "GuideViewController.h"
#import "GuideCollectionViewCell.h"
#import "GuideHomeCollectionViewCell.h"
#import "GuideSmallCollectionViewCell.h"
#import "NetworkHandler.h"
#import "GuideTableViewCell.h"
#import "GuideModel.h"
#import "GuideSmallModel.h"
#import "SearchViewController.h"
#import "GuideMiddle.h"
#import "GuideMiddleViewController.h"
#import "GuideHomeViewController.h"
#import "GuideHomeModel.h"
#import "StrategyDetailsViewController.h"
#import "GuidePlayPictureViewController.h"
#import "ToolModel.h"
#import "ToolDB.h"
#import "TAMacro.h"

@interface GuideViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UIScrollViewDelegate,GuideMiddleDelegate,GuideHomeCollectionViewCellDelegate,GuideCollectionViewCellDelegate,PlayPictureGuideHomeDelegate>
@property (nonatomic, strong) UICollectionView *collectionViewSmall;
@property (nonatomic, strong) NSMutableArray *SmallArray;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) NSMutableArray *arrayID;
@property (nonatomic, strong) UIAlertController *alertController;
@property (nonatomic, strong) UIAlertController *actionSheet;
@property (nonatomic, assign) NSInteger URl;
@property (nonatomic, strong) NSNumber *UR;
@property (nonatomic, copy) NSString *str;
@end

@implementation GuideViewController
-(NSMutableArray *)arrayID{
    
    if (!_arrayID) {
        self.arrayID = [NSMutableArray array];
    }
    return _arrayID;
}

- (NSMutableArray *)array{
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}

-(NSMutableArray *)SmallArray{
    
    if (!_SmallArray) {
        self.SmallArray = [NSMutableArray array];
    }
    return _SmallArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"美丽购";
//    self.navigationItem.titleView.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.980 green:0.458 blue:0.437 alpha:1.000]];
    [self setIsAccessibilityElement:NO];
    [self foundRightButton];
    [self foundBigCollectionView];
    [self foundSmallCollectionView];
    [self analysis];
    [self nightModel];
}
#pragma mark --- 创建右按钮
-(void)foundRightButton{
    UIButton *btn_nav_right = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_nav_right.frame = CGRectMake(SCREEN_WIDTH / 8 * 7, 0, SCREEN_WIDTH / 8 , SCREEN_HEIGHT/  10 );
    [btn_nav_right setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [self.view addSubview:btn_nav_right];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn_nav_right];
    [btn_nav_right addTarget:self action:@selector(clickRight:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark --- 解析最上方按钮
-(void)analysis{
    [NetworkHandler getDataByURLString:@"http://api.liwushuo.com/v2/channels/preset?gender=1&generation=2%20HTTP/1.1" BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"channels"];
        for (NSDictionary *dic in arr) {
            GuideSmallModel *model = [GuideSmallModel modelWithDic:dic];
            self.URl = model.Id;
            self.UR = [NSNumber numberWithInteger:self.URl];
            [self.arrayID addObject:self.UR];
            [self.SmallArray addObject:model];
        }
        [self.collectionViewSmall reloadData];
    }];
}
#pragma mark --- 创建collectionViewBig
-(void)foundBigCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); // 内边距
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionViewBig = [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT/10, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.collectionViewBig.delegate = self;
    self.collectionViewBig.dataSource = self;
    self.collectionViewBig.pagingEnabled = YES;
    [self.view addSubview:_collectionViewBig];
    // 注册cell
    [self.collectionViewBig registerClass:[GuideCollectionViewCell class] forCellWithReuseIdentifier:@"GuideBigCell"];
    [self.collectionViewBig registerClass:[GuideHomeCollectionViewCell class] forCellWithReuseIdentifier:@"GuideHomeCell"];
    [self.view addSubview:self.collectionViewBig];
}
#pragma mark ---collectionViewSmall
-(void)foundSmallCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 6, 40);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); // 内边距
    self.collectionViewSmall = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 40) collectionViewLayout:flowLayout];
    self.collectionViewSmall.showsHorizontalScrollIndicator = NO;
    [self.collectionViewSmall jxl_setDayMode:^(UIView *view) {
        self.collectionViewSmall.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        self.collectionViewSmall.backgroundColor = [UIColor grayColor];
    }];
    self.collectionViewSmall.delegate = self;
    self.collectionViewSmall.dataSource = self;
    [self.view addSubview:_collectionViewSmall];
    [self.collectionViewSmall registerClass:[GuideSmallCollectionViewCell class] forCellWithReuseIdentifier:@"GuideSmallCell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.collectionViewBig) {
        return 14;
    }else{
        return self.SmallArray.count;
    }
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.collectionViewBig) {
        if (indexPath.item == 0) {
            GuideHomeCollectionViewCell *GuideHomeCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuideHomeCell" forIndexPath:indexPath];
            GuideHomeCell.guidemiddleView.delegate = self;
            GuideHomeCell.delegate =self;
            GuideHomeCell.delegates =self;
            return GuideHomeCell;
        }else{
            GuideCollectionViewCell *GuideBigCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuideBigCell" forIndexPath:indexPath];
            GuideBigCell.delegate = self;
            GuideBigCell.arrayModels = self.array;
            return GuideBigCell;
        }
    }else {
        GuideSmallCollectionViewCell *GuideSmallCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuideSmallCell" forIndexPath:indexPath];
        GuideSmallModel *model = self.SmallArray[indexPath.item];
        GuideSmallCell.guideSmallModel = model;
        return GuideSmallCell;
    }
}
// 上方点控制图
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionViewSmall) {
        [self.collectionViewBig setContentOffset:CGPointMake(SCREEN_WIDTH * indexPath.item, -64) animated:YES];
        self.i = indexPath.item - 1;
        if (self.i >= 0) {
            [self getDataByNumber];
        }
    }
}
#pragma mark ---解析
- (void)getDataByNumber {
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    if (manager.isReachableViaWWAN ==YES && manager.isReachableViaWiFi == YES) {
        [ToolDB createTableS];
        NSString *sql = @"SELECT title,cover_image_url,Id FROM t_ACE;";
        NSMutableArray *array = [ToolDB selectFromSql:sql];
        [array removeObjectAtIndex:0];
        self.array = [NSMutableArray arrayWithArray:array];
        [self.collectionViewBig reloadData];
    }else{
        self.array = [NSMutableArray array];
        [NetworkHandler getDataByURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/%@/items?gender=1&limit=20&offset=0&generation=2",self.arrayID[self.i + 1]] BodyString:nil WithDataBlock:^(id result) {
            NSDictionary *dic = [result objectForKey:@"data"];
            NSMutableArray *arr = [dic objectForKey:@"items"];
            for (NSDictionary *dic in arr) {
                GuideModel *model = [GuideModel modelWithDic:dic];
                NSString *sql = [NSString stringWithFormat:@"DELETE FROM t_ACE where title = '%@' and cover_image_url = '%@' and Id = '%ld';", model.title,model.cover_image_url,(long)model.Id];
                [ToolDB dele:sql];
                [ToolDB createTableS];
                NSString *sql2 = [NSString stringWithFormat:@"INSERT INTO t_ACE(title,cover_image_url,ID) VALUES('%@','%@',%ld);", model.title,model.cover_image_url,(long)model.Id];
                [ToolDB execSqlString:sql2];
                [self.array addObject:model];
            }
            [self.collectionViewBig reloadData];
        }];
        
    }
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  {
    if (scrollView == self.collectionViewBig) {
        self.i  = (NSInteger)(scrollView.contentOffset.x / SCREEN_WIDTH);
        self.i = self.i -1;
        if (self.i >= 0) {
            [self getDataByNumber];
        }
    }
}
#pragma mark ---图控制按钮
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.collectionViewBig reloadData];
    NSInteger i = self.collectionViewBig.contentOffset.x / SCREEN_WIDTH;
    self.i = i;
    if (scrollView == self.collectionViewBig && i <= 8 ) {
        [self.collectionViewSmall setContentOffset:CGPointMake((SCREEN_WIDTH /6 + 10)* i, 0) animated:YES];
    }
}
#pragma mark ---右按钮跳转
- (void)clickRight:(UIButton *)sender{
    SearchViewController *searchVC = [[SearchViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}

// 点击首页的tableView协议方法
- (void)didClickSelectItemAtIndexPathGuideHome:(NSIndexPath *)indexPath str:(NSString *)str STR:(NSString *)STR Image:(NSString *)Image {
    self.hidesBottomBarWhenPushed = YES;
    GuideHomeViewController *guideHomeVC = [[GuideHomeViewController alloc]init];
    guideHomeVC.str = str;
    ToolModel *tool =[[ToolModel alloc]init];
    tool.title = STR;
    tool.cover_image_url = Image;
    tool.Id = str;
    guideHomeVC.STR = tool.title;
    guideHomeVC.IMAGE = tool.cover_image_url;
    guideHomeVC.str = tool.Id;
    [self.navigationController pushViewController:guideHomeVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
// 点击其余页的tableView的协议方法
- (void)didClickSelectItemAtIndexPathGuide:(NSIndexPath *)indexPath str:(NSString *)str STR:(NSString *)STR Image:(NSString *)Image {
    self.hidesBottomBarWhenPushed = YES;
    GuideHomeViewController *guideHomeVC = [[GuideHomeViewController alloc]init];
    guideHomeVC.str = str;
    ToolModel *tool =[[ToolModel alloc]init];
    tool.title = STR;
    tool.cover_image_url = Image;
    tool.Id = str;
    guideHomeVC.STR = tool.title;
    guideHomeVC.IMAGE = tool.cover_image_url;
    guideHomeVC.str = tool.Id;
    [self.navigationController pushViewController:guideHomeVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
// 点击轮播图的协议方法
- (void)didClickSelectItemAtIndexPathGuideHomePlayPicture:(NSIndexPath *)indexPath str:(NSString *)str {
    self.hidesBottomBarWhenPushed = YES;
    
    GuidePlayPictureViewController *playPictureVC = [[GuidePlayPictureViewController alloc]init];
    playPictureVC.str = str;
    [self.navigationController pushViewController:playPictureVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
- (void)nightModel {
    // 导航栏夜间模式
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //设置日间和夜间两张状态
    [self.view jxl_setDayMode:^(UIView *view) {
        //设置日间模式
        view.backgroundColor = [UIColor whiteColor];
        
    } nightMode:^(UIView *view) {
        view.backgroundColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];
    [self.navigationController.navigationBar jxl_setDayMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarStyleDefault;
        bar.barTintColor = [UIColor colorWithRed:0.98 green:0.29 blue:0.31 alpha:1.00];
        //        bar.barTintColor = [UIColor redColor];
    } nightMode:^(UIView *view) {
        UINavigationBar *bar = (UINavigationBar *)view;
        //改变状态栏前景颜色
        bar.barStyle = UIBarMetricsDefault;
        bar.barTintColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
    }];
}
@end
